home *** CD-ROM | disk | FTP | other *** search
- /* UnIX to MS-DOS file converter by
- Matthew B. Hornbeck, Director of Technical Services
- Copyright 1989 by Young Minds, Incorporated
- August 8, 1989.
- File : PC_COPY.C
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
-
- #define FALSE 0
- #define TRUE !FALSE
-
- #ifdef __MSDOS__
-
- #include <dir.h>
- #include <dos.h>
- #include <alloc.h>
- #include <stdlib.h>
- #include <sys\stat.h>
-
- #define malloc farmalloc
- #define free farfree
-
- #define SLASH '\\'
-
- #else
-
- #include <malloc.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
-
- #define mystat lstat
-
- #define SLASH '/'
-
- #endif
-
- #ifdef __MSDOS__
- char *prog_name (char *program_name)
- #else
- char *prog_name (program_name)
- char *program_name;
- #endif
-
- { unsigned int len;
-
- len = strlen (program_name) - 1;
- program_name = program_name + len;
- while ((len != 0) && (*program_name != SLASH))
- {
- #ifdef __MSDOS__
- if (*program_name == '.')
- *program_name = '\0';
- #endif
- program_name --;
- len --;
- }
- if (*program_name == SLASH)
- program_name ++;
- return program_name;
- }
-
- #ifdef __MSDOS__
- int copy_file (char *in_name, char *out_name)
- #else
- int copy_file (in_name, out_name)
- char *in_name;
- char *out_name;
- #endif
-
- { FILE *in, *out;
- int chr;
-
- #ifdef __MSDOS__
- if ((in = fopen (in_name, "rb")) == NULL)
- #else
- if ((in = fopen (in_name, "r")) == NULL)
- #endif
- { fprintf (stderr, "Unable to open input file %s!\n", in_name);
- return FALSE;
- }
- #ifdef __MSDOS__
- if ((out = fopen (out_name, "wb")) == NULL)
- #else
- if ((out = fopen (out_name, "w")) == NULL)
- #endif
- { fprintf (stderr, "Unable to open output file %s!\n", out_name);
- fclose (in);
- return FALSE;
- }
- while ((chr = getc (in)) != EOF)
- { if ((chr == '\n') && (putc ('\r', out) == EOF))
- { fprintf (stderr, "Error writing to output file %s!\n", out_name);
- perror ("Problem");
- fclose (out);
- fclose (in);
- return FALSE;
- }
- if (putc (chr, out) == EOF)
- { fprintf (stderr, "Error writing to output file %s!\n", out_name);
- perror ("Problem");
- fclose (out);
- fclose (in);
- return FALSE;
- }
- }
- fclose (out);
- fclose (in);
- return TRUE;
- }
-
- #ifdef __MSDOS__
-
- char *get_name (char *dir_name, int has_wild)
-
- { static struct ffblk *fptr = NULL;
- static struct ffblk fblk;
- static char tmpstr [2048];
-
- if (dir_name != NULL)
- { if (has_wild)
- strcpy (tmpstr, dir_name);
- else
- sprintf (tmpstr, "%s%c*.*", dir_name, SLASH);
- if (findfirst (tmpstr, &fblk, -1 ^ FA_LABEL) == 0)
- { fptr = &fblk;
- return fblk.ff_name;
- }
- fptr = NULL;
- return NULL;
- }
- if (fptr == NULL)
- return NULL;
- if (findnext (&fblk) == 0)
- return fblk.ff_name;
- fptr = NULL;
- return NULL;
- }
-
- #else
-
- char *get_name (dir_name, has_wild)
- char *dir_name;
- int has_wild;
-
- { static DIR *dirp = NULL;
- struct dirent *dir;
-
- if (dir_name != NULL)
- { if ((dirp = opendir (dir_name)) == NULL)
- return NULL;
- }
- if ((dir = readdir (dirp)) == NULL)
- { closedir (dirp);
- dirp = NULL;
- return NULL;
- }
- return dir->d_name;
- }
-
- #endif
-
- typedef struct dir_element {
- struct dir_element *next;
- char *cd_path;
- char *new_path;
- } dir_elem;
-
- static dir_elem *head = NULL, *tail = NULL;
-
- #ifdef __MSDOS__
- int push_dir (char *cd_path, char *new_path)
- #else
- int push_dir (cd_path, new_path)
- char *cd_path, *new_path;
- #endif
-
- { dir_elem *tmp;
-
- if (head == NULL)
- { if ((head = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
- { fprintf (stderr, "Unable to allocate dir_element buffer!\n");
- return FALSE;
- }
- }
- else
- { if ((tail->next = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
- { fprintf (stderr, "Unable to allocate dir_element buffer!\n");
- return FALSE;
- }
- }
- tmp->next = NULL;
- if ((tmp->cd_path = (char *) malloc (strlen (cd_path) + 1)) == NULL)
- { fprintf (stderr, "Unable to allocate cd_path of dir_element!\n");
- return FALSE;
- }
- strcpy (tmp->cd_path, cd_path);
- if ((tmp->new_path = (char *) malloc (strlen (new_path) + 1)) == NULL)
- { fprintf (stderr, "Unable to allocate new_path of dir_element!\n");
- return FALSE;
- }
- strcpy (tmp->new_path, new_path);
- tail = tmp;
- return TRUE;
- }
-
- #ifdef __MSDOS__
- dir_elem *dequeue_dir (void)
- #else
- dir_elem *dequeue_dir ()
- #endif
-
- { dir_elem *tmp;
-
- if (head == NULL)
- { tail = NULL;
- return NULL;
- }
- tmp = head;
- head = tmp->next;
- tmp->next = NULL;
- return tmp;
- }
-
- #ifdef __MSDOS__
- void free_dir (dir_elem *dir)
- #else
- void free_dir (dir)
- dir_elem *dir;
- #endif
-
- {
- free (dir->cd_path);
- free (dir->new_path);
- free (dir);
- }
-
- #ifdef __MSDOS__
- int proc_dir (char *cd_path, char *new_path, int recurse)
- #else
- int proc_dir (cd_path, new_path, recurse)
- char *cd_path;
- char *new_path;
- int recurse;
- #endif
-
- { struct stat statbuf;
- char *fname, *ptr, file_name [1024], new_name [1024];
- int i, has_wild;
-
- has_wild = FALSE;
- #ifdef __MSDOS__
- if (fnsplit (cd_path, new_name, new_name, new_name, new_name) & WILDCARDS)
- has_wild = TRUE;
- #endif
- if (!has_wild && (stat (cd_path, &statbuf) == -1))
- { fprintf (stderr, "Unable to get status of %s!\n", cd_path);
- return FALSE;
- }
- if (!has_wild && ((statbuf.st_mode & S_IFREG) != 0))
- { i = strlen (cd_path) - 1;
- for (fname = cd_path + i; (i != 0) && (*fname != SLASH); i --)
- fname --;
- sprintf (file_name, "%s%c%s", new_path, SLASH, fname);
- return copy_file (cd_path, file_name);
- }
- else if (!has_wild && ((statbuf.st_mode & S_IFDIR) == 0))
- { fprintf (stderr, "Don't understand file type of %s!\n", cd_path);
- return FALSE;
- }
- if ((fname = get_name (cd_path, has_wild)) == NULL)
- { fprintf (stderr, "No such file %s!\n", cd_path);
- return FALSE;
- }
- if (has_wild)
- { i = strlen (cd_path) - 1;
- for (ptr = cd_path + i; (i != 0) && (*ptr != SLASH); i --)
- ptr --;
- *ptr = '\0';
- }
- do
- { if ((strcmp (fname, ".") == 0) || (strcmp (fname, "..") == 0))
- continue;
- sprintf (new_name, "%s%c%s", cd_path, SLASH, fname);
- if (stat (new_name, &statbuf) == -1)
- { fprintf (stderr, "Couldn't get status of %s!\n", new_name);
- continue;
- }
- sprintf (file_name, "%s%c%s", new_path, SLASH, fname);
- if ((statbuf.st_mode & S_IFREG) != 0)
- copy_file (new_name, file_name);
- else if ((statbuf.st_mode & S_IFDIR) != 0)
- { if (!recurse)
- continue;
- #ifdef __MSDOS__
- mkdir (file_name);
- #else
- mkdir (file_name, 0777);
- #endif
- push_dir (new_name, file_name);
- }
- else
- fprintf (stderr, "Unknown file type for %s!\n", new_name);
- } while ((fname = get_name (NULL, FALSE)) != NULL);
- return TRUE;
- }
-
- #ifdef __MSDOS__
- int main (int argc, char *argv [])
- #else
- int main (argc, argv)
- int argc;
- char *argv [];
- #endif
-
- { dir_elem *cur_dir;
- int recurse;
-
- recurse = FALSE;
- if ((argc == 3) && ((strcmp (argv [1], "-r") == 0) || (strcmp (argv [1], "-R") == 0)))
- recurse = TRUE;
- else if (argc != 2)
- { fprintf (stderr, "Usage : %s [-r] cd_pathname\n", prog_name (argv [0]));
- exit (1);
- }
- push_dir (argv [argc - 1], ".");
- while ((cur_dir = dequeue_dir ()) != NULL)
- { proc_dir (cur_dir->cd_path, cur_dir->new_path, recurse);
- free_dir (cur_dir);
- }
- return 0;
- }
-
-